package com.projet.whattowatch.activity;

import java.util.ArrayList;
import java.util.Random;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

import com.projet.whattowatch.R;
import com.projet.whattowatch.adapter.DiscoverAdapter;
import com.projet.whattowatch.adapter.DiscoverFlipperAdapter;
import com.projet.whattowatch.objet.Film;

public class DiscoverFragment extends Fragment
{
	ViewFlipper flipper;
	LinearLayout playingNow;
	LinearLayout comingSoon;
	LinearLayout wantToWatch;
	LinearLayout mostWatched;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		final View rootView = inflater.inflate(R.layout.discover_layout, container, false);
		
        playingNow = (LinearLayout) rootView.findViewById(R.id.playingNowLinearLayout);
        comingSoon = (LinearLayout) rootView.findViewById(R.id.comingSoonLinearLayout);
        wantToWatch = (LinearLayout) rootView.findViewById(R.id.wantToWatchLinearLayout);
        mostWatched = (LinearLayout) rootView.findViewById(R.id.mostWatchedLinearLayout);
    	flipper = (ViewFlipper) rootView.findViewById(R.id.slideShow);
		
 	   	boolean firstrun = getActivity().getSharedPreferences("PREFERENCE", getActivity().MODE_PRIVATE).getBoolean("firstrun", true);
 	    if (firstrun)
 	    {
 	    	new AlertDialog.Builder(getActivity())
 	    	.setTitle("Bienvenue!")
 	    	.setIcon(R.drawable.small_app_icon)
 	    	.setMessage("Bienvenue sur What To Watch !\n" +
 	    				"Envie de regarder un film mais aucune idée concrète ?\n" + 
 	    				"Allez jeter un oeil à notre SmartRequest: il s'agit d'une suggestion intelligente.\nPour pleinement en profiter, n'hésitez pas à ajouter vos films, acteurs, réalisateurs et genres favoris à votre profil.")
 	    	.setNeutralButton("OK", null).show();
 	    }
 	    getActivity().getSharedPreferences("PREFERENCE", getActivity().MODE_PRIVATE).edit().putBoolean("firstrun", false).commit();
        
 	   Handler handlerFlipper = new Handler();
 	   Runnable taskFlipper = new Runnable()
 	   { 
		    @Override
			public void run()
		    {
				ArrayList<String> nameFlipper = new ArrayList<String>();
		        ArrayList<String> imageFlipper = new ArrayList<String>();
		        
		        Random rand = new Random();
				for(int i = rand.nextInt(35 - 0 + 1) + 0; i < MainActivity.mLibFilm.getListFilm().size(); i++)
				{
					nameFlipper.add(MainActivity.mLibFilm.getListFilm().get(i).getNom());
					imageFlipper.add(MainActivity.mLibFilm.getListFilm().get(i).getImageUrl());
				}
				
				DiscoverFlipperAdapter flipperAdapter = new DiscoverFlipperAdapter(getActivity(), nameFlipper, imageFlipper);
				
				final int flipperCount = flipperAdapter.getCount();
				for (int i = 0; i < flipperCount; i++)
				{
					View item = flipperAdapter.getView(i, null, null);
					flipper.addView(item);
				}
				flipper.startFlipping();
		    }
		};
        
        Handler handlerPlayingNow = new Handler();
  	    Runnable taskPlayingNow = new Runnable()
  	    { 
 		    @Override
 			public void run()
 		    {
		        ArrayList<Film> resultPlayingNow = MainActivity.mLibFilm.getListPlayingNow();
		        ArrayList<String> moviePlayinNow = new ArrayList<String>();
		        ArrayList<String> imagePlayingNow = new ArrayList<String>();
		        
		        for(int i = 0; i < resultPlayingNow.size(); i++)
		        {
		        	moviePlayinNow.add(resultPlayingNow.get(i).getNom());
		        	imagePlayingNow.add(resultPlayingNow.get(i).getImageUrl());
		        }
		        
		        DiscoverAdapter playingNowAdapter = new DiscoverAdapter(getActivity(), moviePlayinNow, imagePlayingNow);
		        
		        final int playingNowCount = playingNowAdapter.getCount();
		        for (int j = 0; j < playingNowCount; j++)
		        {
		          View item = playingNowAdapter.getView(j, null, null);
		          playingNow.addView(item);
		        }
 		   }
		};
		
		Handler handlerComingSoon = new Handler();
  	    Runnable taskComingSoon = new Runnable()
  	    { 
 		    @Override
 			public void run()
 		    {
		        ArrayList<Film> resultComingSoon = MainActivity.mLibFilm.getListComingSoon();
		        ArrayList<String> movieComingSoon = new ArrayList<String>();
		        ArrayList<String> imageComingSoon = new ArrayList<String>();
		        
		        for(int i = 0; i < resultComingSoon.size(); i++)
		        {
		        	movieComingSoon.add(resultComingSoon.get(i).getNom());
		        	imageComingSoon.add(resultComingSoon.get(i).getImageUrl());
		        }
		
		        DiscoverAdapter comingSoonAdapter = new DiscoverAdapter(getActivity(), movieComingSoon, imageComingSoon);
		        
		        final int comingSoonCount = comingSoonAdapter.getCount();
		        for (int j = 0; j < comingSoonCount; j++)
		        {
		          View item = comingSoonAdapter.getView(j, null, null);
		          comingSoon.addView(item);
		        }
 		   }
		};
		
		Handler handlerWantToWatch = new Handler();
  	    Runnable taskWantToWatch = new Runnable()
  	    { 
 		    @Override
 			public void run()
 		    {
		        ArrayList<Film> resultWantToWatch = MainActivity.mLibFilm.getListWantToWatch();
		        ArrayList<String> movieWantToWatch = new ArrayList<String>();
		        ArrayList<String> imageWantToWatch = new ArrayList<String>();
		        
		        for(int i = 0; i < resultWantToWatch.size(); i++)
		        {
		        	movieWantToWatch.add(resultWantToWatch.get(i).getNom());
		        	imageWantToWatch.add(resultWantToWatch.get(i).getImageUrl());
		        }
		
		        DiscoverAdapter wantToWatchAdapter = new DiscoverAdapter(getActivity(), movieWantToWatch, imageWantToWatch);
		        
		        final int wantToWatchCount = wantToWatchAdapter.getCount();
		        for (int j = 0; j < wantToWatchCount; j++)
		        {
		          View item = wantToWatchAdapter.getView(j, null, null);
		          wantToWatch.addView(item);
		        }
 		   }
		};

		Handler handlerMostWatched = new Handler();
  	    Runnable taskMostWatched = new Runnable()
  	    { 
 		    @Override
 			public void run()
 		    {
		        ArrayList<Film> resultMostWatched = MainActivity.mLibFilm.getListMostWatched();
		        ArrayList<String> movieMostWatched = new ArrayList<String>();
		        ArrayList<String> imageMostWatched = new ArrayList<String>();
		        
		        for(int i = 0; i < resultMostWatched.size(); i++)
		        {
		        	movieMostWatched.add(resultMostWatched.get(i).getNom());
		        	imageMostWatched.add(resultMostWatched.get(i).getImageUrl());
		        }
		
		        DiscoverAdapter mostWatchedAdapter = new DiscoverAdapter(getActivity(), movieMostWatched, imageMostWatched);
		        
		        final int mostWatchedCount = mostWatchedAdapter.getCount();
		        for (int j = 0; j < mostWatchedCount; j++)
		        {
		          View item = mostWatchedAdapter.getView(j, null, null);
		          mostWatched.addView(item);
		        }
 		   }
		};
		
		handlerFlipper.post(taskFlipper);
		handlerPlayingNow.post(taskPlayingNow);
		handlerComingSoon.post(taskComingSoon);
		handlerMostWatched.post(taskMostWatched);
		handlerWantToWatch.post(taskWantToWatch);

		return rootView;
    }
}